package com.ccl.iot.publics;import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.inputmethod.InputMethodManager;

public class Window {
	public static final int KCONTEXT_TYPE_UNKNOWN = 0;
	public static final int KCONTEXT_TYPE_APPLICATION = 1;
	public static final int KCONTEXT_TYPE_SERVICE = 2;
	public static final int KCONTEXT_TYPE_ACTIVITY = 3;
	public static final int KCONTEXT_TYPE_DIALOG = 4;
	public static final int KCONTEXT_TYPE_THEME = 4;
	
	/**
	 * 获取传入context的类型
	 * @param aClass
	 * @return
	 */
	public static int GetContextType(Class<?> aClass){
		Class<?> tClass = aClass;

		do{
			String tTarget = tClass.getName();
			
			if (tTarget.equals("android.app.Application")){
				return KCONTEXT_TYPE_APPLICATION;
			}else if (tTarget.equals("android.app.Service")){
				return KCONTEXT_TYPE_SERVICE;
			}else if (tTarget.equals("android.app.Activity")){
				return KCONTEXT_TYPE_ACTIVITY;
			}else if (tTarget.equals("android.app.Dialog")){
				return KCONTEXT_TYPE_DIALOG;
			}else if (tTarget.equals("android.view.ContextThemeWrapper")){
				return KCONTEXT_TYPE_THEME;
			}
		}while ((tClass = tClass.getSuperclass()) != null);
		
		return KCONTEXT_TYPE_UNKNOWN;		
	}
	
	public static int GetContextType(Context aContext){
		if (aContext != null){
			return GetContextType(aContext.getClass());
		}
		
		return KCONTEXT_TYPE_UNKNOWN;		
	}
	
	public static boolean IsIntentContext(Context aContext){
		int tContextType = GetContextType(aContext);
		
		return tContextType == KCONTEXT_TYPE_ACTIVITY || 
					tContextType == KCONTEXT_TYPE_DIALOG || 
					tContextType == KCONTEXT_TYPE_THEME;
	}
	
	public static void ShowWindow(Context aContext, Class<?> aClass, int aFlags, Object ...aExtras){
		switch (GetContextType(aClass)){
			case KCONTEXT_TYPE_ACTIVITY:{
				ShowActivity(aContext, aClass, aFlags, aExtras);
				
				break;
			}
			case KCONTEXT_TYPE_DIALOG:{
				ShowDialog(aContext, aClass);
				
				break;
			}
		}
	}
	
	/**
	 * 
	 * @param aContext
	 * @param aClass - Activity.class
	 * @param aFlags - Intent.FLAG_XXX
	 * @param aExtras - Can be Intent/Bundle or pairs name and value
	 */
	public static void ShowActivity(Context aContext,Class<?> aClass, int aFlags, Object ...aExtras){
		Intent newPage = new Intent();
		newPage.setClass(aContext, aClass);
		newPage.setFlags(aFlags);
		
		if (aExtras != null && aExtras.length > 0){
			if (aExtras.length == 1){
				if (aExtras[0] instanceof Intent){
					newPage.putExtras((Intent)aExtras[0]);
				}else if (aExtras[0] instanceof Bundle){
					newPage.putExtras((Bundle)aExtras[0]);
				}
			}else if (aExtras.length % 2 == 0){
				for (int i = 0; i < aExtras.length; i += 2){
					if (aExtras[i] instanceof String){
						if (aExtras[i + 1] instanceof String){
							newPage.putExtra((String) aExtras[i], (String) aExtras[i + 1]);
						}else if (aExtras[i + 1] instanceof Long){
							newPage.putExtra((String) aExtras[i], (Long) aExtras[i + 1]);
						}else if (aExtras[i + 1] instanceof Boolean){
							newPage.putExtra((String) aExtras[i], (Boolean) aExtras[i + 1]);
						}else if (aExtras[i + 1] instanceof Integer){
							newPage.putExtra((String) aExtras[i], (Integer) aExtras[i + 1]);
						}else if (aExtras[i + 1] instanceof int[]){
							newPage.putExtra((String) aExtras[i], (int []) aExtras[i + 1]);
						}
					}
				}				
			}
		}
		
		aContext.startActivity(newPage);
	}
	
	public static void ShowDialog(Context aContext, Class<?> aClass){
		if (IsIntentContext(aContext)){
			try {
				Constructor<?> tConstructor[] = aClass.getDeclaredConstructors();
				
				if (tConstructor.length > 0){
					for (int i = 0; i < tConstructor.length; i++){
						Class<?> tParams[] = tConstructor[i].getParameterTypes();
						
						if (tParams.length == 1){
							if (tParams[0].getName().equals("android.content.Context")){
								Dialog tDialog = (Dialog) tConstructor[i].newInstance(aContext);
								
								if (tDialog != null){
									tDialog.show();
								}
								
								break;
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void ShowInputDialog(Context aContext){
		if (aContext != null){
			final Context tContext = aContext;
			
			new Handler().postDelayed(new Runnable() {
	            @Override
	            public void run() {
	                    InputMethodManager imm = (InputMethodManager) tContext.getSystemService(Context.INPUT_METHOD_SERVICE);
	                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
	                }
	        }, 500);			
		}
	}
	
	public static boolean IsShowing(Activity aActivity){
		if (aActivity != null){
			RunningTaskInfo tTask = GetTopTask(aActivity);
			
			if (tTask != null){
				return tTask.id == aActivity.getTaskId();
			}
		}
		
		return false;
	}
	
	public static RunningTaskInfo GetTopTask(Context aContext){
		if (aContext != null){
			ActivityManager tActivityManager = (ActivityManager)aContext.getSystemService(Context.ACTIVITY_SERVICE);
			
			if (tActivityManager != null){
			    List<RunningTaskInfo> tTasks = tActivityManager.getRunningTasks(1); 
				
			    if (tTasks != null && tTasks.size() > 0){
			    	return tTasks.get(0);
			    }
			}
		}
		
		return null;
	}
	
	private static ArrayList<String> gHomes = null;
	
	public static boolean StayInHome(Context aContext){
		RunningTaskInfo tTask = GetTopTask(aContext);
		
		if (tTask != null){
			if (gHomes == null){
			    PackageManager tManager = aContext.getPackageManager(); 
			    Intent intent = new Intent(Intent.ACTION_MAIN); 
			    
				gHomes = new ArrayList<String>();	
			    intent.addCategory(Intent.CATEGORY_HOME);  
			    
			    List<ResolveInfo> tInfos = tManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  
			    for(ResolveInfo tInfo : tInfos){  
			        gHomes.add(tInfo.activityInfo.packageName);  
			    }  
			}
		    
		    return gHomes.contains(tTask.topActivity.getPackageName());	
		}

		return true;
	}
}
